
import { useRouter,useRoute } from 'vue-router'
import { ref,reactive } from 'vue'
import { detailsItem  } from '@/store/tabParams'
import bus from '@/bus'
import Model from '@/http/api'
import Store from '@/store/index'
const api = new Model()
import { ElMessage } from 'element-plus'
function handleDetails() {
  // 获取路由器实例
  const router = useRouter()
  const route = useRoute()
  const detailsId = route.params.id
  //详情对象
  // const detailsObj = detailsItem.data.details || JSON.parse(sessionStorage.getItem("detailsObj"))
  const detailsObj =  reactive({
    data:{}
  })
  const optionDetails = reactive({
    id: detailsId
  })
  const isShow = ref(false)
  async function getDetails(){
    api.getDetail(optionDetails).then(({data}) => {
      if(data.data){
        detailsObj.data = data.data
        sessionStorage.setItem("detailsObj",JSON.stringify(data.data))
        //判断是否是自己上传的（是否可以编辑）
        if (data.data.author.loginname == 'ForevSky') {
          isShow.value = true
        } else {
          isShow.value = false
        }
        bus.emit("updataInfo",{
          username: data.data.author
        })
      }
    })
  }
  //初始化加载详情
  getDetails();


  //控制删除弹窗
  const dialogVisible = ref(false)
  const collected = ref(false)
  //编辑主题
  function editOrInsert() {
    sessionStorage.setItem('index', 6)
    bus.emit('detailsToEdit', 6)
    //跳转到编辑页
    router.push({
      name: 'NewInsert',
      params: {
        from: 'edit',
        tab: 'dev',
        title: detailsObj.data.title,
        content: detailsObj.data.content,
        topic_id: detailsObj.data.id,
      },
    },() => {
    //通过点击详情跳转操作栏目设置样式
    })
  }
  const options = reactive({
    data:{
      accesstoken: Store.state.token,
      topic_id: detailsObj.data.id
    }
  })
  function deleDig(){
    dialogVisible.value = true
  }
  // 删除主题
  function deleteColumn() {
    api.deleteColumn(options).then(res => {
      console.log(res);
      if(res.success){
        ElMessage.success('操作成功！')
      }
    })
    router.back()
  }
  return {
    detailsObj,
    isShow,
    collected,
    dialogVisible,
    deleDig,
    editOrInsert,
    deleteColumn
  }
}

export {
  handleDetails
}